{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    资产权限
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">资产管理</a></li>
        <li class="active">资产权限</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    修改资产权限
{% endblock %}

{% block body_form %}
    <form name="assetForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">基本信息</p>

            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>名称</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="name" value="{{ name }}" placeholder="请填写权限名称" data-rule="名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>用户</label>
                            <div class="col-lg-8">
                                <select name="user_ids" class="form-control select2" data-placeholder="请填写用户" data-rule="用户: required;" autocomplete="off">
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>资产</label>
                            <div class="col-lg-8">
                                <select name="asset_ids" class="form-control select2" data-placeholder="请选择资产" data-rule="资产: required;">
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>系统用户</label>
                            <div class="col-lg-8">
                                <select name="system_user_ids" class="form-control select2" data-placeholder="请选择系统用户" data-rule="系统用户: required;">
                                </select>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>是否激活</label>
                            <div class="col-lg-8">
                                <div class="checkbox">
                                    {% if is_active %}
                                        <input id="radio-active-show" class="magic-radio" type="radio" name="is_active" value="1" checked>
                                        <label for="radio-active-show">是</label>

                                        <input id="radio-active-none" class="magic-radio" type="radio" name="is_active" value="0">
                                        <label for="radio-active-none">否</label>
                                    {% else %}
                                        <input id="radio-active-show" class="magic-radio" type="radio" name="is_active" value="1">
                                        <label for="radio-active-show">是</label>

                                        <input id="radio-active-none" class="magic-radio" type="radio" name="is_active" value="0" checked>
                                        <label for="radio-active-none">否</label>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>动作</label>
                            <div class="col-lg-8">
                                <div class="checkbox">

                                    <input id="checkbox-action-connect" class="magic-checkbox" type="checkbox" name="actions" value="connect" {% if 'connect' in actions %} checked {% endif %}>
                                    <label for="checkbox-action-connect">连接</label>

                                    <input id="checkbox-action-upload" class="magic-checkbox" type="checkbox" name="actions" value="upload_file" {% if 'upload_file' in actions %} checked {% endif %}>
                                    <label for="checkbox-action-upload">上传</label>

                                    <input id="checkbox-action-download" class="magic-checkbox" type="checkbox" name="actions" value="download_file" {% if 'download_file' in actions %} checked {% endif %}>
                                    <label for="checkbox-action-download">下载</label>
                                </div>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label">备注</label>
                            <div class="col-lg-8">
                                <textarea class="form-control" name="comment" rows="8" placeholder="请填写备注信息">{{ comment }}</textarea>
                            </div>
                        </div>
                    </fieldset>
                </div>

            </div>
        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right submit-btn" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">
        $(function () {
            bees.initServerSideSelect2($('select[name="asset_ids"]'), {
                url: '{{ url_for("assets.api-asset-list") }}',
                selected: '{{ select_assets }}'
            }, { multiple:true});
            bees.initServerSideSelect2($('select[name="system_user_ids"]'), {
                url: '{{ url_for("assets.api-systemuser-list") }}',
                selected: '{{ select_system_users }}'
            }, { multiple:true});
            bees.initServerSideSelect2($('select[name="user_ids"]'), {
                url: '{{ url_for("users.api-user-list") }}',
                selected: '{{ select_users }}'
            }, { multiple:true});
        });


        $(function () {
            var $formEle = $('form[name="assetForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                var name = $('input[name="name"]').val();
                var user_ids = $('select[name="user_ids"]').val() || [];
                var asset_ids = $('select[name="asset_ids"]').val() || [];
                var system_user_ids = $('select[name="system_user_ids"]').val() || [];
                var is_active = $('input[name="is_active"]').val();
                var comment = $('textarea[name="comment"]').val() || '';
                var actions = [];
                $('input[name="actions"]').each(function () {
                    if($(this).prop('checked')){
                        actions.push($(this).val());
                    }
                });
                $.ajax({
                    type: 'post',
                    url: '{{ url_for("assets.auth-update", pk=default_pk) }}'.replace(bees.defaultPK, '{{ id }}'),
                    data: {
                        name: name,
                        user_ids: user_ids.join(','),
                        asset_ids: asset_ids.join(','),
                        system_user_ids: system_user_ids.join(','),
                        actions: actions.join(','),
                        is_active: is_active,
                        comment: comment
                    },
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '更新成功'
                            });
                            window.location.href = '{{ url_for("assets.auth-list") }}'
                        }
                    }
                });
            });

        })

    </script>

{% endblock %}
